package com.xiaoxi.checkpasswordserver.domain;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName LengthCheck
 * @Description
 * @Author xiaoxi
 * @Date 2022/7/11 21:02
 * @Version 1.0
 */
@Component
public class LengthCheck implements IPasswordCheck {

    @Value("${bms.password-check.check-min-length}")
    private String minNum;
    @Value("${bms.password-check.check-max-length}")
    private String maxNum;

    @Override
    public CheckResult check(String password, CheckResult checkResult) {

        boolean flag = false;

        if (StringUtils.isBlank(maxNum)) {
            minNum = StringUtils.isBlank(minNum) ? "0" : minNum;
            if (password.length() >= Integer.parseInt(minNum)) {
                flag = true;
            }
        } else {
            minNum = StringUtils.isBlank(minNum) ? "0" : minNum;
            if (password.length() >= Integer.parseInt(minNum) && password.length() <= Integer.parseInt(maxNum)) {
                flag = true;
            }
        }

        if (!flag) {
            checkResult.getErrorReason().add("密码长度应大于等于：" + minNum + ",小于等于：" + maxNum);
        }

        return checkResult;
    }
}
